package com.sr.config;

import com.sr.constant.Constant;
import com.sr.interceptor.GlobalInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

/**
 * @author xiaochi
 * @date 2022/4/27 18:10
 * @desc WebMvcConfig
 */
@Configuration
@RequiredArgsConstructor(onConstructor = @__)
public class WebMvcConfig implements WebMvcConfigurer {

    private final GlobalInterceptor globalInterceptor;

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.clear();
        converters.add(new MappingJackson2HttpMessageConverter());
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(globalInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("doc.html","/webjars/**","/favicon.ico","/v2/api-docs","/v2/**","/swagger-resources","/**/*.html","/**/*.js","/**/*.css")
                .excludePathPatterns("/api/login","/api/logout","/api/captcha")
                .excludePathPatterns(Constant.Api.BACK_PREFIX+"/**");
    }
}
